<template>
  <el-dialog
    v-model="dialogVisible"
    :title="title"
    width="40%"
    :before-close="handleClose"
  >
    <div class="user-dialog">
      <el-form
        inline
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="用户名" prop="username" class="form-item-style">
          <el-input
            placeholder="输入用户名"
            style="width: 100%"
            v-model="ruleForm.username"
          />
        </el-form-item>
        <el-form-item label="姓名" prop="realname" class="form-item-style">
          <el-input
            placeholder="输入姓名"
            style="width: 100%"
            v-model="ruleForm.realname"
          />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile" class="form-item-style">
          <el-input
            placeholder="输入手机号"
            style="width: 100%"
            v-model="ruleForm.mobile"
          />
        </el-form-item>
        <el-form-item label="部门" prop="dept_id" class="form-item-style">
          <el-tree-select
            default-expand-all
            node-key="id"
            filterable
            v-model="ruleForm.dept_id"
            :data="treeData"
            check-strictly
            :render-after-expand="false"
            :props="{
              label: 'name',
            }"
            style="width: 100% !important"
            placeholder="选择部门"
          >
          </el-tree-select>
        </el-form-item>
        <el-form-item label="职务/岗位" prop="position" class="form-item-style">
          <el-input
            placeholder="输入职务（岗位）"
            style="width: 100%"
            v-model="ruleForm.position"
          />
        </el-form-item>
        <el-form-item
          label="密码"
          v-if="!ruleForm.id"
          prop="password"
          class="form-item-style"
        >
          <el-input
            placeholder="输入密码"
            style="width: 100%"
            v-model="ruleForm.password"
          />
        </el-form-item>
        <el-form-item label="是否在职" prop="is_quit" class="form-item-style">
          <el-radio-group
            v-model="ruleForm.is_quit"
            :disabled="ruleForm.id ? true : false"
          >
            <el-radio :label="0">在职</el-radio>
            <el-radio :label="1">离职</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="性别" prop="gender" class="form-item-style">
          <el-radio-group v-model="ruleForm.gender">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="角色" prop="roles" class="form-item-style">
          <el-select
            v-model="ruleForm.roles"
            multiple
            placeholder="选择角色"
            style="width: 100%"
          >
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="submit"> 提交 </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { addApi, saveApi } from "@/plugins/api/system/user";
import { listApi } from "@/plugins/api/system/role";

import { ElMessage } from "element-plus";
const props = defineProps({
  treeData: {
    type: Array,
    default: [],
  },
});
let ruleForm = ref({});
let rules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  realname: [{ required: true, message: "请输入姓名", trigger: "blur" }],
  mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }],
  dept_id: [
    {
      required: true,
      message: "请选择部门",
      trigger: "change",
    },
  ],
  position: [{ required: true, message: "请输入职务/岗位", trigger: "blur" }],
  password: [{ required: true, message: "请填写密码", trigger: "blur" }],
  is_quit: [
    {
      required: true,
      message: "请选择是否在职",
      trigger: "change",
    },
  ],
  gender: [
    {
      required: true,
      message: "请选择性别",
      trigger: "change",
    },
  ],
  roles: [
    {
      required: true,
      message: "请选择角色",
      trigger: "change",
    },
  ],
};
let dialogVisible = ref(false);
function handleClose(done) {
  // dialogVisible.value = false;
  ElMessageBox.confirm("是否确定关闭此对话框?")
    .then(() => {
      done();
    })
    .catch(() => {
      // catch error
    });
}
let title = ref("");
let ruleFormRef = ref(null);
function initFun(type, row) {
  if (ruleFormRef.value) {
    ruleFormRef.value.resetFields();
  }
  if (type == "add") {
    title.value = "新增员工";
    ruleForm.value = {};
    ruleForm.value.is_quit = 0;
    ruleForm.value.gender = "男";
  } else {
    title.value = "编辑员工";
    ruleForm.value = Object.assign({}, row);
    if (row.roles) {
      ruleForm.value.roles = [];
      let arr = row.roles.split(",").map((ele) => {
        return parseInt(ele);
      });

      ruleForm.value.roles = arr;
    }
  }

  dialogVisible.value = true;
}
const emit = defineEmits(["refreshList"]);
function submit() {
  if (!ruleFormRef.value) return;
  ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      let obj = Object.assign({}, ruleForm.value);
      obj.roles = obj.roles.join(",");
      if (!obj.id) {
        addApi(obj).then((data) => {
          ElMessage.success("新增成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      } else {
        saveApi(obj).then((data) => {
          ElMessage.success("修改成功！");
          dialogVisible.value = false;
          emit("refreshList");
        });
      }
    }
  });
}
let roleList = ref([]);
function getRoles() {
  listApi({ status: 1 }).then((data) => {
    roleList.value = data;
  });
}
onMounted(() => {
  getRoles();
});
defineExpose({
  initFun,
});
</script>
<style scoped lang="scss">
.user-dialog {
  .form-item-style {
    width: 50%;
    margin-right: 0;

    :deep(.el-input) {
      width: 100%;
    }
  }
}
</style>
